package org.brisling.sysman.queryService;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.service.AbstractBaseQueryService;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.JsonUtil;
import org.brisling.sysman.domain.UploadFiles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("/upload/query")
public class QueryUploadService extends AbstractBaseQueryService<UploadFiles>{
	
	
	
	@Autowired
	private QueryDictionaryService dicService;
	
	public QueryUploadService(){
		this.moduleName = ConstSysParam.MODULE_UPLOAD;
		this.subSystem = ConstSysParam.SUBSYSTEM_SYSMAN;
		this.tableName = ConstSysParam.VIEW_PREFIX +this.getTableName(UploadFiles.class);
		this.clazz = UploadFiles.class;
		
		typeMap = this.getTypeMap(clazz);
		
		orderMap.put("sortedid","desc");			
		
	}
	
	@PersistenceContext(unitName=ConstSysParam.SUBSYSTEM_SYSMAN)
	@Autowired
	protected void setEntityManager(EntityManager entityManager){
		this.entityManager = entityManager;		
	}
	
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return this.getBizMainPage();
	}
	
	/* (non-Javadoc)
	 * @see org.brisling.service.sysman.DepartmentServiceInf#getEditPage()
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return this.getBizEditPage();
	}
	
	@RequestMapping("/uploadfivePage.action")
	public ModelAndView getUploadPage(){
		return new ModelAndView("/sysman/sysman_upload/uploadfive");
	}
	
	@RequestMapping("/imageShowPage.action")
	public ModelAndView getImageShowPage(){
		return new ModelAndView("/sysman/sysman_upload/imageShow");
	}
	
	@RequestMapping("/getListDomain.action")
	public ListDomain<UploadFiles> getListDomain(
			@RequestBody(required=false) String body){
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,false);
	}
	
	@RequestMapping("/getInitListDomain.action")
	public ListDomain<UploadFiles> getInitListDomain(
			@RequestBody(required=false) String body){
		
		Map<String,Object> paramMap = this.getBodyParam(body);
		
		if (paramMap.containsKey("isSupplyEditPage")) {
			return getSupplyListDomainByConditionAndOrder(paramMap);
		}
		
		return super.getListDomainByConditionAndOrder(body, typeMap, orderMap,false);
	}
	
	private ListDomain<UploadFiles> getSupplyListDomainByConditionAndOrder(Map<String, Object> paramMap) {		
		
		String sql = "select * from %1$s where bizcode in (%2$s) and bizno = %3$s order by sortedid";
		if (paramMap != null && paramMap.containsKey("bizcode_in") && paramMap.containsKey("bizno")){
			sql = String.format(sql, this.tableName, paramMap.get("bizcode_in"), paramMap.get("bizno"));
		}
		
		
		List<UploadFiles> list =  super.getAllList(sql);
		
		List<LinkedHashMap<String, String>> mapList = getSupplyArchiveConfig();
		
		LinkedHashMap<String,String> archiveMap = new LinkedHashMap<String,String>();
		
		for(LinkedHashMap<String,String> m:mapList){
			archiveMap.put(m.get("typecode"), m.get("typename"));
		}
//		map.put("supply-businesslicence", "营业执照");
//		map.put("supply-authorizecertification", "法人授权书");
//		map.put("supply-socialsecurity", "社保证明");
//		map.put("supply-education", "被授权人培训证");
//		map.put("supply-nobriberecord", "无行贿记录证明");
//		map.put("supply-aftersaleservice", "售后服务承诺书");
//		map.put("supply-honestcontract", "廉洁购销合同");
		
	//	map.put("supply-badinfomation", "不良记录");
		
		for (UploadFiles e : list) {
			String _bizcode = e.getBizcode().toLowerCase();
			if (archiveMap.containsKey(_bizcode)) {
				archiveMap.remove(_bizcode);
			}
		}
		
		//如果附件为空，剔除请选择类别项，避免附件类别中显示该类别附件
		
		archiveMap.remove("supply-undefined");
		
		
		for (String key : archiveMap.keySet()) {
			UploadFiles u = makeNewUploadFiles(archiveMap.get(key));
			if(u!=null)list.add(u);
		}
		
		
		ListDomain<UploadFiles> lists = new ListDomain<UploadFiles>();

		for (UploadFiles e : list) {
			lists.getRows().add(e);
		}
		
		
		return lists;
	}
	
	private UploadFiles makeNewUploadFiles(String value) {
		if(value!=null){
			
			UploadFiles e = new UploadFiles();
			e.setFilename(value);
			return e;
			
		}
		return null;
	}
	
	/**
	 * 获取附件列表
	 * @param map
	 * @return
	 */
	public ListDomain<UploadFiles> getListDomain(
			Map<String,Object> map){
		
		return super.getListDomainByConditionAndOrder(null,map, typeMap, orderMap,false);
	}
	
	/**
	 * 获取附件列表
	 * @param map
	 * @return
	 */
	public List<UploadFiles> getList(
			Map<String,String> map){
		
		String sql = "select * from %1$s where bizno in ('%2$s') and bizcode = '%3$s' order by id";
		
		String sql1 = "select * from %1$s where bizno in (%2$s) and bizcode = '%3$s' order by id";
		if(map!=null){
			if(map.containsKey("bizno_in") && map.containsKey("bizcode")){
				String ids = map.get("bizno_in");
				
				if( (ids!=null) && (ids.length()>0) && (ids.indexOf("'")==0) ){
					sql =  sql1;
				}
				String bizcode = map.get("bizcode");
				
				sql = String.format(sql, this.tableName,ids,bizcode);
				return super.getAllList(sql);
			}
		}
		
		return new ArrayList<UploadFiles>();
		
	}
	
	@RequestMapping("/getTreeListDomain.action")
	public ListDomain<UploadFiles> getTreeListDomain(@RequestParam(value="id",required=false) Long id){
		
		return this.getPagedTreeListDomain(id);
		
	}
	
	@RequestMapping("/getOne.action")
	public UploadFiles getOne(@RequestParam(value="id",required=false) Long id){
		return super.getOne(id);
	}

	/**
	 * 根据用户唯一编码查找用户信息
	 * @param identify	用户唯一编码
	 * @return 用户信息记录
	 */
	@RequestMapping("/getOneByIdentify.action")
	public UploadFiles getOneByField(@RequestParam(value="field",required=false) String field,@RequestParam(value="fieldValue",required=false) String fieldValue){	
		return super.getOneByIdentify(field,fieldValue);
	}
	
	
	
	
	/**
	 * 获取供应商附件类型定义
	 * @return
	 */
	@RequestMapping("/getSupplyArchiveConfig.action")
	public List<LinkedHashMap<String,String>> getSupplyArchiveConfig(){
		
		
		List<LinkedHashMap<String,String>> list = new ArrayList<LinkedHashMap<String,String>>();
		
//		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		String _config = dicService.getSqlByBizcode("supply_archive_config");
				
		if(_config!=null){
			 list = JsonUtil.bodyToListLinkedHashMap(_config);
			
		}else{	
			//缺省附件类型配置
			String[] _defaultConfig = {
					"supply-undefined,请选择类别,0",
					"supply-businesslicence,营业执照,1",
					"supply-authorizecertification,法人授权书,2",
					"supply-socialsecurity,社保证明,3",
					"supply-education,被授权人培训证,4",
					"supply-nobriberecord,无行贿记录证明,5",
					"supply-aftersaleservice,售后服务承诺书,6",
					"supply-honestcontract,廉洁购销合同,7"};
			
			for(String s:_defaultConfig){
				String[] _c = s.split(",");
				if(_c.length>=3){
					LinkedHashMap<String,String> map = new LinkedHashMap<String,String>();
					map.put("typecode", _c[0]);
					map.put("typename", _c[1]);
					map.put("sortedid", _c[2]);
					list.add(map);
				}
				
			}
		}
		
		return list;
	}
	
	/**
	 * 获取供应商附件类型定义map
	 * @return
	 */
	@RequestMapping("/getSupplyArchiveConfigMap.action")
	public LinkedHashMap<String,String> getSupplyArchiveConfigMap(){
		
		List<LinkedHashMap<String,String>> mapList = this.getSupplyArchiveConfig();
		LinkedHashMap<String,String> archiveMap = new LinkedHashMap<String,String>();
		
		for(LinkedHashMap<String,String> m:mapList){
			archiveMap.put(m.get("typecode"), m.get("typename"));
		}
		
		return archiveMap;
	}
}
